function gOrL(a, b) -- returns proper pos/neg multiplier

	if a > b then
		return 1
	elseif a < b then
		return -1
	else
		return 1
	end


end

function thread_func(func) -- runs arg string func as a new thread

	local gf = love.filesystem.newFileData(func, "gen_func", "file")

	local thread = love.thread.newThread(love.timer.getTime().."", gf)

end

function fadeColor(
  time, prologue, attack, sustain, decay, epilogue,
  fade_in_r, fade_in_g, fade_in_b,
  fade_out_r, fade_out_g, fade_out_b
)
  -- [0, prologue)
  if time < prologue then
    return
      fade_in_r,
      fade_in_g,
      fade_in_b,
      255
  end
   
  -- (prologue, prologue + attack]
  time = time - prologue
  if time < attack then
    return
      fade_in_r,
      fade_in_g,
      fade_in_b,
      ( math.cos( time / attack * math.pi ) + 1 ) / 2 * 255
  end
   
  -- (prologue + attack, prologue + attack + sustain]
  time = time - attack
  if time < sustain then
    return
      fade_in_r,
      fade_in_g,
      fade_in_b,
      0
  end
   
  -- (prologue + attack + sustain, prologue + attack + sustain + decay]
  time = time - sustain
  if time < decay then
    return
      fade_out_r,
      fade_out_g,
      fade_out_b,
      255 - ( ( math.cos( time / decay * math.pi ) + 1 ) / 2 * 255 )
  end
   
  -- (prologue + attack + sustain + decay, prologue + attack + sustain + decay + epilogue]
  time = time - decay
  if time < epilogue then
    return
      fade_out_r,
      fade_out_g,
      fade_out_b,
      255
  end
   
  -- End of fading, return all nils.
end



function findRotation(x1,y1,x2,y2)

  local t = -math.deg(math.atan2(x2-x1,y2-y1))
  if t < 0 then t = t + 360 end;
  return t;

end

function round(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end

function tablelength(T)

  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

function table.val_to_str ( v )
  if "string" == type( v ) then
    v = string.gsub( v, "\n", "\\n" )
    if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
      return "'" .. v .. "'"
    end
    return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
  else
    return "table" == type( v ) and table.tostring( v ) or
      tostring( v )
  end
end

function table.key_to_str ( k )
  if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
    return k
  else
    return "[" .. table.val_to_str( k ) .. "]"
  end
end

function table.tostring( tbl )
  local result, done = {}, {}
  for k, v in ipairs( tbl ) do
    table.insert( result, table.val_to_str( v ) )
    done[ k ] = true
  end
  for k, v in pairs( tbl ) do
    if not done[ k ] then
      table.insert( result,
        table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
    end
  end
  return "{" .. table.concat( result, "," ) .. "}"
end

--[[
   Author: Julio Manuel Fernandez-Diaz
   Date:   January 12, 2007
   (For Lua 5.1)

   Modified slightly by RiciLake to avoid the unnecessary table traversal in tablecount()

   Formats tables with cycles recursively to any depth.
   The output is returned as a string.
   References to other tables are shown as values.
   Self references are indicated.

   The string returned is "Lua code", which can be procesed
   (in the case in which indent is composed by spaces or "--").
   Userdata and function keys and values are shown as strings,
   which logically are exactly not equivalent to the original code.

   This routine can serve for pretty formating tables with
   proper indentations, apart from printing them:

      print(table.show(t, "t"))   -- a typical use

   Heavily based on "Saving tables with cycles", PIL2, p. 113.

   Arguments:
      t is the table.
      name is the name of the table (optional)
      indent is a first indentation (optional).
--]]
function table.show(t, name, indent)
local cart     -- a container
   local autoref  -- for self references

   --[[ counts the number of elements in a table
   local function tablecount(t)
      local n = 0
      for _, _ in pairs(t) do n = n+1 end
      return n
   end
   ]]
   -- (RiciLake) returns true if the table is empty
   local function isemptytable(t) return next(t) == nil end

   local function basicSerialize (o)
      local so = tostring(o)
      if type(o) == "function" then
         local info = debug.getinfo(o, "S")
         -- info.name is nil because o is not a calling level
         if info.what == "C" then
            return string.format("%q", so .. ", C function")
         else
            -- the information is defined through lines
            return string.format("%q", so .. ", defined in (" ..
                info.linedefined .. "-" .. info.lastlinedefined ..
                ")" .. info.source)
         end
      elseif type(o) == "number" or type(o) == "boolean" then
         return so
      else
         return string.format("%q", so)
      end
   end

   local function addtocart (value, name, indent, saved, field)
      indent = indent or ""
      saved = saved or {}
      field = field or name

      cart = cart .. indent .. field

      if type(value) ~= "table" then
         cart = cart .. " = " .. basicSerialize(value) .. ";\n"
      else
         if saved[value] then
            cart = cart .. " = {}; -- " .. saved[value]
                        .. " (self reference)\n"
            autoref = autoref ..  name .. " = " .. saved[value] .. ";\n"
         else
            saved[value] = name
            --if tablecount(value) == 0 then
            if isemptytable(value) then
               cart = cart .. " = {};\n"
            else
               cart = cart .. " = {\n"
               for k, v in pairs(value) do
                  k = basicSerialize(k)
                  local fname = string.format("%s[%s]", name, k)
                  field = string.format("[%s]", k)
                  -- three spaces between levels
                  addtocart(v, fname, indent .. "   ", saved, field)
               end
               cart = cart .. indent .. "};\n"
            end
         end
      end
   end

   name = name or "__unnamed__"
   if type(t) ~= "table" then
      return name .. " = " .. basicSerialize(t)
   end
   cart, autoref = "", ""
   addtocart(t, name, indent)
   return cart .. autoref
end

function append_strings(str1, str2)

	str3 = str1 .. str2
	return str3

end

-- additive range
-- x1, y1 set as primary range context
-- returns values ready to be added to
-- existing positions (properly returns
-- positive/negative number accordingly)
function additive_range(x1, y1, x2, y2)

	local x3 = (x1 - x2)*-1
	local y3 = (y1 - y2)*-1


	return x3, y3


end

